package com.sheng.center.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.sheng.center.entity.Staff;

public class StaffDaoImpl implements StaffDao {

	private SessionFactory sessionFactory;
	@Override
	public void add( Staff  staff) {
		staff.setIsDelete(0);
			sessionFactory.getCurrentSession().save( staff);
	}

	@Override
	public void edit( Staff  staff) {
		staff.setIsDelete(0);
			sessionFactory.getCurrentSession().update( staff);
	}

	@Override
	public void delete( Staff  staff) {
		staff.setIsDelete(1);
		sessionFactory.getCurrentSession().update( staff);
	}
	
	
	public  String verify( Staff  staff) {
		return (String) sessionFactory.getCurrentSession().createQuery("from User where username='" + staff.getUsername() +"'").uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List< Staff> findAllList( Staff model) {
		String hql = "from  Staff where 1=1  and is_delete=0";
		if(null != model.getUsername()){
			hql = hql + " and  username like '%"+model.getUsername()+"%'";
		}
		Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        List list = query.list();  
        return list;
	}
	public  Staff findById( Staff  staff) {
		Query query = sessionFactory.getCurrentSession().createQuery("from  Staff where id='"+ staff.getId()+"'");  
		List list = query.list();  
		if (null != list && 0 < list.size()) {
			staff = ( Staff) list.get(0);
		}
		return  staff;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
